<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 *
 * @Date: 2017-06-08
 * @Time: 19:50
 * @Author: cdkay
 * @Email: network@iyuanma.net
 *
 * @File： Menu.php
 */
class Menu extends MY_Controller {

    private $menu_tree;
    public function __construct()
    {
        parent::__construct();

        $this->load->library('Tree');
        $this->load->model('system/Menu_model');

        // 获取菜单数据
        $menu = $this->Menu_model->get_all_menu();
        $this->menu_tree = Tree::makeTreeForHtml($menu, array(
            'primary_key' => 'menu_id',
            'parent_key' => 'menu_parentid',
        ));
    }

    /**
     * index
     * 管理后台系统菜单列表
     */
    public function index()
    {

        $data['menu']  = $this->menu_tree;

        $this->load->view('system/menu/index', $data);
    }

    /**
     * add
     * 添加管理后台系统菜单
     *
     * @param int $parent_id 父级菜单id
     */
    public function add($parent_id=null)
    {
        $data['menu']  = $this->menu_tree;

        // 添加子菜单时父级菜单id标识
        $data['parent_id'] = $parent_id;
        $this->load->view('system/menu/add', $data);
    }

    /**
     * edit
     * 编辑管理后台系统菜单
     */
    public function edit($menu_id)
    {
        $data['menu_tree']  = $this->menu_tree;

        $data['menu'] = $this->Menu_model->get_single_menu($menu_id);


        $this->load->view('system/menu/edit', $data);
    }

    /**
     * save
     * 保存管理后台系统菜单数据 添加|编辑
     */
    public function save()
    {
        if (!IS_POST) exit();

        $result = $this->Menu_model->menu_save();

        exit(json_encode($result));
    }

    /**
     * del
     * 删除管理后台系统菜单
     */
    public function del()
    {
        $result = $this->Menu_model->menu_delete();

        exit(json_encode($result));
    }

    /**
     * sort
     * 管理后台系统菜单排序
     */
    public function sort()
    {
        if (!IS_POST) exit();

        $result = $this->Menu_model->menu_sort();

        if ($result) {
            $result = array('status'=>1, 'msg'=>'操作成功', 'result'=>array('jumpUrl'=>$_SERVER['HTTP_REFERER']));
        }

        exit(json_encode($result));
    }
}